<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<?
require_once '../classes/ClasseDataManager.php';
require_once '../classes/ConnectionDB.php';

$db = ConnectionDB::getInstance()->connectToDB();

?>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Ricerca studenti</title>
        <script src="../glassboxLatest/glassbox.2.0.0/javascripts/prototype.js" type="text/javascript"></script>
        <script src="../glassboxLatest/glassbox.2.0.0/javascripts/scriptaculous/effects.js" type="text/javascript"></script>
        <script src="../glassboxLatest/glassbox.2.0.0/javascripts/glassbox/glassbox.js" type="text/javascript"></script>
        <style type="text/css">
            @import url("../css/style.css");


            table{
                width: 100%;

            }

            td{
                text-align: center;
            }

            #searchForm {
                padding-top: 3%;
                margin-left: -5px;
            }



        </style>
        <script type="text/javascript">
            path_to_root_dir = "../glassboxLatest/glassbox.2.0.0/";
            var listaStudenti;
            var listaClassi;
            var numItem=10;
            var currentIndex=0;

            window.onload = function(){
                var searchForm = new GlassBox();
                searchForm.init( 'searchForm', '640px', '190px', 'hidden','',true, false);
                searchForm.ipos();
                searchForm.appear();

                var results = new GlassBox();
                results.init( 'results', '1240px', '590px', '','',true,false);
                results.ipos();
                results.appear();


            }

            function handlerClassi()
            {
                if(this.readyState == 4 && this.status == 200)
                {
                    //          alert("resp : " + this.responseText);
                    var resp = eval(this.responseText);
                    listaClassi = new Array();
                    if(resp!=null)
                    {
                        var str="";
                        str+='<option value="">Tutte le classi</option>';
                        for(i=0;i<resp.length; i++)
                        {
                            listaClassi[i] = resp[i];

                            str +='<option value="'+listaClassi[i].codice+'">'+listaClassi[i].classe + " "+listaClassi[i].sezione+ " "+ listaClassi[i].descrizioneIndirizzo+'</option>';
                            document.getElementById("classe").innerHTML=str;
                        }
                    }
                }
            }

            function loadClassi()
            {
                var client = xXMLHttpRequest();
                client.onreadystatechange = handlerClassi;

                var param = "operation=loadClassi";
                client.open("POST", "searchStudentsListener.php", true);
                client.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

                client.send(param);
            }

            function xXMLHttpRequest()
            {

                var client = null;
                if (window.XMLHttpRequest)
                {
                    client=new XMLHttpRequest();
                }
                // code for IE
                else if (window.ActiveXObject)
                {
                    client=new ActiveXObject("Microsoft.XMLHTTP");
                }

                return client;
            }



            function handler3()
            {
                if(this.readyState == 4 && this.status == 200)
                {
                    loadClassi();
                    find();
                }
            }


            function find(indNav)
            {
                var client = xXMLHttpRequest();
                client.onreadystatechange = function()
                {
                    if(this.readyState == 4 && this.status == 200)
                    {
                        //          alert("resp : " + this.responseText);
                        var resp = eval(this.responseText);
                        listaStudenti = new Array();
                        if(resp!=null)
                        {
                            for(i=0;i<resp.length; i++)
                            {
                                listaStudenti[i] = resp[i];
                            }
                        }
                        if(indNav == null)
                            manageNavi(0);
                        else
                            manageNavi(indNav);


                    }
                    else if (this.readyState == 4 && this.status != 200)
                    {
                        // fetched the wrong page or network error...
                        alert("error this.status : " + this.status );

                    }
                };
                var nome = document.getElementById('nome').value;
                var cognome = document.getElementById('cognome').value;
                var classe =document.getElementById('classe').value;

                var param = "nome="+ nome +
                    "&cognome="+cognome+
                    "&classe="+classe;
                client.open("POST", "searchStudentsListener.php", true);
                client.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

                client.send(param);

            }

            function eliminaStudente(codice, page)
            {
                var client = xXMLHttpRequest();
                client.onreadystatechange = function()
                {
                    if(this.readyState == 4 && this.status == 200)
                    {
                        find(page);
                    }
                };

                var param = "codice="+ codice+"&operation=elimina";
                client.open("POST", "searchStudentsListener.php", true);
                client.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

                client.send(param);
            }

            function eliminaClasse()
            {
                var codiceClasse= document.getElementById("classe").value;
                if(codiceClasse!="")
                {
                    var client = xXMLHttpRequest();

                    client.onreadystatechange = handler3;

                    var param = "codice="+ codiceClasse+"&operation=eliminaClasse";
                    client.open("POST", "searchStudentsListener.php", true);
                    client.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

                    client.send(param);
                }
                else
                    alert("Seleziona una classe per eliminarla");

            }

            function handlerCredenziali()
            {

            }

            function genera(codice, indNav)
            {
                var password = generaPassword();

                var client = xXMLHttpRequest();

                client.onreadystatechange = function(){
                    if(this.readyState == 4 && this.status == 200)
                    {
                        find(indNav);
                    }
                };

                var param = "codice="+ codice+"&operation=generaCredenziali&password="+password;
                client.open("POST", "searchStudentsListener.php", true);
                client.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

                client.send(param);

            }

            function generaPassword()
            {
                N_Caratteri = 10;
                Stringa = "";
                for (I=0;I<N_Caratteri;I++)
                {
                    do
                    {
                        N = Math.floor(Math.random()*74)+48;
                    }
                    while(!(((N >= 48) && (N <= 57)) || ((N >= 65) && (N <= 90)) || ((N >= 97) && (N <= 122))));

                    Stringa = Stringa+String.fromCharCode(N);
                }
                return Stringa;
            }

            function forzaPassword(codice, page)
            {
                var client = xXMLHttpRequest();
                client.onreadystatechange = function()
                {
                    if(this.readyState == 4 && this.status == 200)
                    {
                        //          alert("resp : " + this.responseText);
                        var resp = eval(this.responseText);
                        listaStudenti = new Array();
                        if(resp!=null)
                        {
                            for(i=0;i<resp.length; i++)
                            {
                                listaStudenti[i] = resp[i];
                            }
                        }
                        if(indNav == null)
                            manageNavi(0);
                        else
                            manageNavi(page);


                    }
                    else if (this.readyState == 4 && this.status != 200)
                    {
                        // fetched the wrong page or network error...
                        alert("error this.status : " + this.status );

                    }
                };

                var pass = window.prompt("Indirizzo","Inserisci la nuova password");
                if(pass!=null && pass!="Inserisci la nuova password")
                {
                    var param = "password="+ pass+"&codice="+codice+"&operation=forcePassword";

                    client.open("POST", "../utente/nuovoOperatoreListener.php", true);
                    client.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

                    client.send(param);
                }

            }
            function manageNavi(ind)
            {
                var tmp= new Array();

                var str="<table border='1'  cellpadding='5'>"
                    +"<tr>"
                    +"<th>Codice Studente</th>"
                    +"<th>Username</th>"
                    +"<th>Nome</th>"
                    +"<th>Cognome</th>"
                    +"<th>Data creazione</th>"
                    +"<th>Email</th>"
                    +"<th>Classe</th>"
                    +"<th>Sezione</th>"
                    +"<th>Indirizzo</th>"
                    +"<th>Info test</th>"
                    +"<th>Elimina</th>"
                    +"<th>Genera credenziali</th>"
                    +"<th>Forza password</th>"
                    +"</tr>";
                var page=ind;
                var indCurrenct=0;
                while(indCurrenct<numItem)
                {
                    //                    alert("idn " + ind);
                    if(ind<listaStudenti.length)
                    {
                        tmp=listaStudenti[ind];
                        str+="<tr><td>"+tmp.codiceStudente+"</td>"+
                            "<td>"+tmp.username+"</td>"+
                            "<td>"+tmp.nome+"</td>"+
                            "<td>"+tmp.cognome+"</td>"+
                            "<td>"+tmp.dataCreazione+"</td>";
                        if(tmp.email=="")
                            str += "<td>&nbsp;</td>";
                        else
                            str += "<td>"+tmp.email+"</td>";

                        str += "<td>"+tmp.ordClasse+"</td>"+
                            "<td>"+tmp.sezione+"</td>"+
                            "<td>"+tmp.descrizioneIndirizzo+"</td>"+
                            "<td><img src='../images/icons/PNG/Magnifying Glass.png' height='30' width='30' title='Risultati' border='0' onclick='javascript:location.href=\"reportLiveTestByStudent.php?codiceStudente="+ tmp.codiceStudente+"\"' /></td>"+
                            "<td><input type='button' value='Elimina' onclick='javascript:eliminaStudente("+tmp.codiceStudente+", "+page+")' /></td>"+
                            "<td><input type='button' value='Genera credenziali' onclick='javascript:genera("+tmp.codiceStudente+", "+page+")' /></td>"+
                            "<td><input type='button' value='Forza password' onclick='javascript:forzaPassword("+tmp.codiceStudente+", "+page+")' /></td></tr>";



                    }
                    ind++;
                    indCurrenct++;
                }
                str +="</table>";
                var numTotItem = listaStudenti.length;
                var numPag = Math.ceil(numTotItem / numItem);
                if(numPag > 1)
                {
                    str +="<div id='navigator'><center>";
                    var current = 0;
                    var toPrint=1
                    for(k=1;k<=numPag;k++)
                    {
                        str+="<a href='javascript:manageNavi("+current+")'>"+toPrint+"</a>&nbsp;&nbsp;";
                        current= k * numItem;
                        toPrint = toPrint+1;
                    }
                    str+="</center></div>";
                }
                document.getElementById('resultsCont').innerHTML=str;
            }


        </script>
    </head>
    <body>
        <script>loadClassi();</script>
        <?php include '../portal/header.php'; ?>
        <center>
            <div id="searchForm">
                <table>
                    <tr>
                        <th>Ricerca studente</th>
                    </tr>
                    <tr>
                        <td>Nome:</td>
                        <td><input type="text" name="nome" id="nome" /></td>
                    </tr>
                    <tr>
                        <td>Cognome:</td>
                        <td><input type="text" name="cognome" id="cognome" /></td>
                    </tr>
                    <tr>
                        <td>Classe:</td>
                        <td>
                            <select name="classe" id="classe">


                            </select>

                            <input type="button" value="Elimina classe" onclick="eliminaClasse()" />
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">&nbsp;</td>
                        <td>
                            <input type="button" value="Cerca" onclick="find();" />
                        </td>
                    </tr>


                </table>

            </div>
            <div id="results"><div id="resultsCont"></div></div>
        </center>
    </body>
</html>
